package com.yhf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhf.dto.StagePto;
import com.yhf.mapper.PMapper;
import com.yhf.mapper.StageMapper;
import com.yhf.model.Project;
import com.yhf.model.Stage;
import com.yhf.service.PService;
import com.yhf.service.StageService;
import com.yhf.utils.PageResult;
import com.yhf.utils.QueryPageBean;
import com.yhf.utils.Result;
import com.yhf.vo.ProjectEmag;
import com.yhf.vo.StageProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.time.LocalDate;
import java.util.List;

@Service
public class StageServiceImpl extends ServiceImpl<StageMapper, Stage> implements StageService {
    @Autowired
    StageMapper stageMapper;
    @Autowired
    PMapper pMapper;
    @Override
    public PageResult pageList(QueryPageBean queryPageBean){
        Page<StagePto> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<StagePto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        IPage<StagePto> page1 = stageMapper.selectAll(page, queryWrapper);
        PageResult pageResult = new PageResult();
        pageResult.setRows(page1.getRecords());
        pageResult.setTotal(page1.getTotal());
        return pageResult;
    }

    /**
     * 返回所有的项目，如梭有阶段id，返回该阶段的项目
     * @param sid
     * @return
     */
    @Override
    public Result findSp(String sid) {
        Result result = new Result();
        StageProject stageProject = new StageProject();
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1);
        List<Project> projects = pMapper.selectList(queryWrapper);
        ProjectEmag[] projectEmags = new ProjectEmag[projects.size()];
        for (int i = 0;i<projects.size();i++) {
            ProjectEmag projectEmag = new ProjectEmag();
            projectEmag.setValue(projects.get(i).getPid());
            projectEmag.setLabel(projects.get(i).getPname());
            projectEmags[i] = projectEmag;
        }
        stageProject.setProjectEmags(projectEmags);
        result.setData(stageProject);
        if (sid != null) {
            Stage stage = stageMapper.selectById(sid);
            stageProject.setChecked(stage.getPid());
        }
        result.setFlag(true);
        result.setMessage("success");
        return result;
    }

    @Override
    public Result Add(Stage stage) {
        int insert = stageMapper.insert(stage);
        if (insert > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result findStageById(String sid) {
        Result result = new Result();
        Stage stage = stageMapper.selectById(sid);
        result.setData(stage);
        result.setMessage("success");
        result.setFlag(true);
        return result;
    }

    @Override
    public Result editByStage(Stage stage) {
        stage.setUpdatetime(new Date());
        int i = stageMapper.updateById(stage);
        if (i > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result deleteBySid(String sid) {
        Stage stage = new Stage();
        stage.setStatus(0);
        stage.setSid(sid);
        int delete = stageMapper.updateById(stage);
        if (delete >0) {
            return Result.success();
        }
        return Result.fail();
    }

}
